package cn.dengxin.springcloud.helloservice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * Author:   dengxin
 * Date:     2020/4/30 12:01 下午
 */
@RestController
@Slf4j
public class HelloController {

    @Value("${server.port}")
    private String port;

    @Autowired
    DiscoveryClient client;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() {
        log.info("/hello,host:localhost:{}", port);
        return "Hello World";
    }
//    public String hello() throws Exception {
//        //让线程随机等待一段时间
//        int sleepTime = new Random().nextInt(4000);
//        log.info("sleepTime:" + sleepTime);
//        Thread.sleep(sleepTime);
//        log.info("/hello,host:localhost:{}", port);
//        return "Hello World";
//    }

    @RequestMapping(value = "/hello1", method = RequestMethod.GET)
    public String hello(@RequestParam String name) {
        return "hello1 " + name;
    }

    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public User hello(@RequestHeader String name, @RequestHeader Integer age) {
        return new User(name, age);
    }

    @RequestMapping(value = "/hello3", method = RequestMethod.POST)
    public String hello(@RequestBody User user) {
        return "Hello3 " + user.getName() + ", " + user.getAge();
    }


}
